package cocurrency.callable;

import java.util.concurrent.*;

/**
 * Created by luzl on 4/6/2017.
 */
public class CompletionServiceTest {

    public static void main(String[] args){

        ExecutorService executor=Executors.newFixedThreadPool(1);
        CompletionService<Integer> completionService=new ExecutorCompletionService<Integer>(executor);

        for(int i=0;i<10;i++) {
            CallableThread thread = new CallableThread();
            completionService.submit(thread);
        }
        executor.shutdown();

        int loop=0;
        int round=0;
        while(loop<10){
            System.out.println("round =>"+round);
            Future<Integer> result=null;
            try {
                result = completionService.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                if (result != null) {
                    System.out.println("result is => " + result.get(1, TimeUnit.MILLISECONDS).intValue());
                    loop++;
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
            round++;
        }

    }
}
